
--------------------------------------------------

MsgN('--------------------------------------------------')

local nStartTime = CurTime()

MsgN('Loading "ds_menu.lua"')

--------------------------------------------------

// CreateClientConVar('cl_menu_sng_debug' , '0' , false , false)

--------------------------------------------------

g_SelectedMap = nil

g_ds_menu = {}

--------------------------------------------------

function g_ds_menu.check( sString , sPattern )
	return ( string.find( string.lower( string.Trim( sString ) ) , string.lower( string.Trim( sPattern ) ) ) )
end

--------------------------------------------------

if (file.Find( "maps_data/favorites.txt" )) then
	g_ds_menu.tMapFavorites = ( KeyValuesToTable( file.Read( "maps_data/favorites.txt" ) or "" ) or {} )
else
	g_ds_menu.tMapFavorites = {}
	file.Write( "maps_data/favorites.txt" , TableToKeyValues( g_ds_menu.tMapFavorites ) )
end

g_ds_menu.tAddons = {}
for k,v in pairs(GetAddonList()) do
	g_ds_menu.tAddons[v] = GetAddonInfo( v )
end
g_ds_menu.tGameModes = GetGamemodes()
g_ds_menu.tMapFiles = file.Find( "maps/*.bsp", true )
g_ds_menu.tCategoryFiles = file.Find( "maps_data/*.txt", true )
g_ds_menu.tMapsList = {}
g_ds_menu.tCategories = {
	// ['All'] = {['*']=''}, -- All maps are added to this category
	// ['Other'] = {},
}

g_ds_menu.sFavoritesFile = 'ds_menu/sng_favorites.txt'
if (#file.Find( g_ds_menu.sFavoritesFile ) > 0) then
	g_ds_menu.tFavoriteMaps = KeyValuesToTable( file.Read( g_ds_menu.sFavoritesFile ) )
else
	g_ds_menu.tFavoriteMaps = {}
end

function g_ds_menu.func_AddFavoriteMap( sMap )
	MsgN( 'func_AddFavoriteMap: ' , sMap )
	table.insert( g_ds_menu.tFavoriteMaps , sMap )
end

function g_ds_menu.func_RemoveFavoriteMap( sMap )
	MsgN( 'func_RemoveFavoriteMap: ' , sMap )
	
	local tFavTemp = {}
	
	for nIndex , sFav in pairs( g_ds_menu.tFavoriteMaps ) do
		if ( sFav != sMap ) then
			table.insert( tFavTemp , sFav )
		end
	end
	
	g_ds_menu.tFavoriteMaps = tFavTemp
end

function g_ds_menu.func_IsFavoriteMap( sMap )
	return table.HasValue( g_ds_menu.tFavoriteMaps , sMap )
end

function g_ds_menu.func_GetFavoriteMaps()
	return ( g_ds_menu.tFavoriteMaps or {} )
end

function g_ds_menu.func_SaveFavoriteMaps()
	file.Write( g_ds_menu.sFavoritesFile , TableToKeyValues( g_ds_menu.tFavoriteMaps ) )
end

g_ds_menu.func_SaveFavoriteMaps()

g_ds_menu.tMapsIndex = {}
g_ds_menu.tMaps = {}
g_ds_menu.tSettings = {}
g_ds_menu.tSettings.BasicFiles = file.Find( "maps_data/settings/basic/*.txt", true )
g_ds_menu.tSettings.AdvancedFiles = file.Find( "maps_data/settings/advanced/*.txt", true )
g_ds_menu.tSettings.GamemodesFiles = {}

g_ds_menu.tSettings.Basic = {}
g_ds_menu.tSettings.Advanced = {}
g_ds_menu.tSettings.Addons = {}
g_ds_menu.tSettings.Gamemode = {}

g_ds_menu.bLog = false
g_ds_menu.tLog = {}
g_ds_menu.sLog_Prefix = 'ds_menu.lua'
g_ds_menu.func_Log = function( ... )
	local sString = '[' .. ( g_ds_menu.sLog_Prefix ) .. '] ' .. string.Implode( '' , {...} or {} )
	
	if (!g_ds_menu.tLog[ g_ds_menu.sLog_Prefix ]) then g_ds_menu.tLog[ g_ds_menu.sLog_Prefix ] = {} end
	
	table.insert( g_ds_menu.tLog[ g_ds_menu.sLog_Prefix ] , sString )
	
	if (g_ds_menu.bLog) then MsgN( sString ) end
end
g_ds_menu.func_LogDisplay = function( ... )
	local sString = string.Implode( '' , {...} or {} )
	
	MsgN( sString )
end

--------------------------------------------------

MsgN()

g_ds_menu.sLog_Prefix = 'ds_menu/getmaps.lua'
include('ds_menu/getmaps.lua')
// Create Map / Category Tables

MsgN()

g_ds_menu.sLog_Prefix = 'ds_menu/getsettings.lua'
include('ds_menu/getsettings.lua')
// Create Settings Table

MsgN()

g_ds_menu.sLog_Prefix = 'ds_menu/vgui_startnewgame.lua'
include('ds_menu/vgui_startnewgame.lua')
// Start New Game

MsgN()

--------------------------------------------------

concommand.Add('cl_sng_debug' , function(nilNothing , sCommand , tArguments)
	for nIndex , sMsg in pairs( g_ds_menu.tLog [ tArguments[1] ] or {} ) do
		g_ds_menu.func_LogDisplay( sMsg )
	end
end)

--------------------------------------------------

MsgN( 'Loaded "ds_menu.lua" in ' , ( math.Round( ( CurTime() - nStartTime ) * 10000 ) / 10000 ) , ' seconds' )

MsgN('--------------------------------------------------')

--------------------------------------------------
